home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 22 / Cream of the Crop 22.iso / program / snip9611.zip / PUSHDIR.C < prev    next >
C/C++ Source or Header  |  1996-11-24  |  5KB  |  181 lines

  1. /* +++Date last modified: 28-Sep-1996 */
  2.  
  3. /*
  4. **  PushDir() and PopDir()
  5. **
  6. **  Original Copyright 1988-1991 by Bob Stout as part of
  7. **  the MicroFirm Function Library (MFL)
  8. **
  9. **  The user is granted a free limited license to use this source file
  10. **  to create royalty-free programs, subject to the terms of the
  11. **  license restrictions specified in the LICENSE.MFL file.
  12. */
  13.  
  14. #include <dos.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <ctype.h>
  18. #include "dosfiles.h"
  19. #include "unistd.h"
  20.  
  21. #define DIR_STACK_SIZE  8
  22. #define MAX_FLEN        67
  23.  
  24. static int  PushDir_stack_ptr;
  25. static char PushDir_stack[DIR_STACK_SIZE][MAX_FLEN];
  26.  
  27. /*
  28. **  PushDir()
  29. **
  30. **  Like chdir(), except a drive may be specified and the old directory
  31. **  is saved.
  32. **
  33. **  Arguments: 1 - newdir, the buffer containing the new directory name
  34. **
  35. **  Returns:  -1 - stack overflow
  36. **             0 - error
  37. **             1 - success, still on same drive
  38. **             2 - success, changed drive
  39. **
  40. **  Side effects: Converts name in newdir to upper case and prepends
  41. **                a drive letter.
  42. **
  43. **  CAUTION: Since a drive will be prepended to newdir, its buffer
  44. **           should be at least MAX_FLEN long.
  45. */
  46.  
  47. int PushDir(char *newdir)
  48. {
  49.       char pname[MAX_FLEN];
  50.       char drive[3];
  51.       char *target = &pname[2];
  52.       int  new_drv = 0, ercode = 0;
  53.       static int init = 0;
  54.  
  55.       if (!init)
  56.             PushDir_stack_ptr = init = -1;
  57.       if (DIR_STACK_SIZE <= ++PushDir_stack_ptr)
  58.       {
  59.             ercode = -1;
  60.             goto ErrEx;
  61.       }
  62.       getcwd(PushDir_stack[PushDir_stack_ptr], MAX_FLEN);
  63.       strupr(PushDir_stack[PushDir_stack_ptr]);
  64.       strncpy(drive, PushDir_stack[PushDir_stack_ptr], 2);
  65.       drive[2] = '\0';
  66.       if (':' == newdir[1])
  67.       {     /* If a drive is specified                                  */
  68.             strupr(newdir);
  69.             strcpy(pname, newdir);
  70.             if (strchr(target, ':'))      /* if filename is illegal     */
  71.                   goto ErrEx;
  72.             if (*drive != *newdir)
  73.             {
  74.                   if (Error_ == chdrv(newdir[0] - 'A'))
  75.                   {     /* If the drive is invalid                      */
  76.                         goto ErrEx;
  77.                   }
  78.                   else  new_drv = 1;
  79.             }
  80.       }
  81.       else
  82.       {     /* If a drive isn't specified                               */
  83.             if (!strchr(strupr(newdir), ':'))
  84.             {     /* If legal filename                                  */
  85.                   strcpy(pname, drive);
  86.                   strcat(pname, newdir);
  87.                   strcpy(newdir, pname);
  88.             }
  89.             else
  90.             {     /* If filename is illegal                             */
  91.                   goto ErrEx;
  92.             }
  93.       }
  94.  
  95.       if (*target)
  96.       {
  97.             if (chdir(target))
  98.             {
  99.                   if (1 == new_drv) /* We already changed drives        */
  100.                         chdrv(*drive - 'A');    /* Go home before exit  */
  101.                   goto ErrEx;
  102.             }
  103.       }
  104.       return (new_drv + 1);
  105. ErrEx:
  106.       --PushDir_stack_ptr;
  107.       return (ercode);
  108. }
  109.  
  110. /*
  111. **  PopDir()
  112. **
  113. **  Like chdir(), except goes to the drive/directory specified on the
  114. **  top of the PushDir stack.
  115. **
  116. **  Arguments: none
  117. **
  118. **  Returns:  -1 - stack empty
  119. **             0 - error - stack pointer unchanged
  120. **             1 - success, still on same drive
  121. **             2 - success, changed drive
  122. **
  123. **  Side effects: none
  124. **
  125. **  CAUTION: chdir() or chdrv() should not be called between PushDir-
  126. **           PopDir calls.
  127. */
  128.  
  129. int PopDir(void)
  130. {
  131.       char I_am_here[MAX_FLEN], target_drv, *target;
  132.       int new_drv = 0;
  133.  
  134.       if (0 > PushDir_stack_ptr)
  135.             return -1;
  136.       getcwd(I_am_here, MAX_FLEN);
  137.       target = &PushDir_stack[PushDir_stack_ptr][2];
  138.       target_drv = PushDir_stack[PushDir_stack_ptr][0];
  139.       if (I_am_here[0] != target_drv)
  140.       {
  141.             if (Error_ == chdrv(target_drv - 'A'))
  142.                   return 0;
  143.             new_drv = 1;
  144.       }
  145.       if (!chdir(target))
  146.       {
  147.             --PushDir_stack_ptr;
  148.             return (1 + new_drv);
  149.       }
  150.       else  return 0;
  151. }
  152.  
  153. /*
  154. **  isdir()
  155. **
  156. **  Checks to see if a drive and/or path are a valid directory.
  157. **
  158. **  Arguments: 1 - dir, the buffer containing the new directory name
  159. **
  160. **  Returns: Error_ - push/popdir stack overflow
  161. **           False_ - not a valid directory
  162. **           True_  - valid directory
  163. **
  164. **  Side effects: Converts name in dir to upper case and prepends a
  165. **                drive letter.
  166. **
  167. **  CAUTION: Since a drive will be prepended to newdir, it's buffer
  168. **           should be at least MAX_FLEN long.
  169. */
  170.  
  171. int isdir(char *dir)
  172. {
  173.       int ercode;
  174.  
  175.       if (-1 == (ercode = PushDir(dir)))
  176.             return ercode;
  177.       if (ercode)
  178.             PopDir();
  179.       return TOBOOL(ercode);
  180. }
  181.